/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.search.strategy.social;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.groupme.model.Identifiable;
import net.groupme.model.Resource;
import net.groupme.search.strategy.model.AbstractWeightedIdentifiable;
import net.groupme.search.strategy.model.WeightedIdentifiableComparatorByWeight;
import net.groupme.search.strategy.model.WeightedResource;

/**
 * created on May 28, 2008
 * 
 * This class provides some utility methods for ranking stuff, e.g. re-ranking
 * a set of {@link Resource} instances according to a {@link List} of {@link WeightedResource} instances.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.1 $ $Date: 2008-05-28 16:13:38 $
 */
public class RankingUtility {

	/**
	 * (Generic version)
	 * This method re-ranks a list of given {@link Identifiable} instances according to an overall ranking given as ordered list of {@link AbstractWeightedIdentifiable} instances.
	 * 
	 * @param folksonomyEntities the items to re-rank
	 * @param rankingOfAllEntities the ranking according to which the re-ranking should be done
	 * @return a re-ranked list of given {@link Identifiable} instances. Items that are not contained in the ranking are excluded from the returned list.
	 */
	public static List<? extends AbstractWeightedIdentifiable> reRankIdentifiables(List<? extends Identifiable> folksonomyEntities, List<? extends AbstractWeightedIdentifiable> rankingOfAllEntities){
		List<AbstractWeightedIdentifiable> reRankedItems = new ArrayList<AbstractWeightedIdentifiable>();
		for(Identifiable item: folksonomyEntities){
			if(rankingOfAllEntities.contains(item)){
				reRankedItems.add(rankingOfAllEntities.get(rankingOfAllEntities.indexOf(item)));
			}
		}
		Collections.sort(reRankedItems, new WeightedIdentifiableComparatorByWeight());
		return reRankedItems;
	}
	
	/**
	 * (Resource version)
	 * This method re-ranks a list of given {@link Resource} instances according to an overall ranking given as ordered list of {@link WeightedResource} instances.
	 * 
	 * @param folksonomyEntities the items to re-rank
	 * @param rankingOfAllEntities the ranking according to which the re-ranking should be done
	 * @return a re-ranked list of given {@link Identifiable} instances. Items that are not contained in the ranking are excluded from the returned list.
	 */
	public static List<WeightedResource> reRankResources(List<Resource> resources, List<WeightedResource> rankingOfAllEntities){
		List<WeightedResource> reRankedItems = new ArrayList<WeightedResource>();
		List<WeightedResource> rankedItems = new ArrayList<WeightedResource>();
		for(Resource item: resources){
			rankedItems.add(new WeightedResource(item, 0.0));
		}
		for(WeightedResource item: rankedItems){
			if(rankingOfAllEntities.contains(item)){
				reRankedItems.add(rankingOfAllEntities.get(rankingOfAllEntities.indexOf(item)));
			}
		}
		Collections.sort(reRankedItems, new WeightedIdentifiableComparatorByWeight());
		return reRankedItems;
	}
}
